"""
闭包
可以保存外层函数的变量作为状态
"""

def outer(logo:str):
    def inner(name):
        print(f"<{logo}>{name}<{logo}>")

    return inner

fn1=outer("br")
fn1("你好")
fn1("再见")

fn2=outer("p")
fn2("hello")
fn2("世界")

print("-------------------------------------------")


def outer2(a):
    def add(b):
        nonlocal a
        a+=b
        print(f"a={a}")

    return add

fn3=outer2(3)
fn3(2)
fn3(5)

print("-------------------------------------------")


def account_create(init_balance=0):
    def atm(money,deposit=True):
        nonlocal init_balance
        if deposit:
            init_balance+=money
            print(f"存款：+{money},余额：{init_balance}")
        else:
            init_balance-=money
            print(f"存款：-{money}，余额：{init_balance}")

    return atm

atm=account_create()
atm(120)
atm(200)
atm(150,False)



